/**
 * Copyright (C) 2010 Krishna Sundaresan <calvinkrishy@gmail.com>
 *      
 * This file is part of webos-datecalculator application.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

function FirstAssistant(){
}

FirstAssistant.prototype.resetButtonTap = function(){
    this.init();
    this.updateModels();
}

FirstAssistant.prototype.updateModels = function(){

    this.controller.setWidgetModel('date_field', this.model);
    this.controller.setWidgetModel('text_field', this.txt_model);
    $('date_title').update(this.date_title)
    
}

FirstAssistant.prototype.init = function(){

    var todayDate = new Date()
    this.model = {
        date: todayDate,
    }
    
    this.text_attr = {
        focus: true,
        multiline: false,
        maxlength: 4,
        hintText: '0',
        modelProperty: 'initial',
        modifierState: Mojo.Widget.numLock,
        charsAllow: function(valueEntered){
        
            Mojo.Log.info('Validating ' + valueEntered);
            
            var toAllow = (valueEntered == 27 || (valueEntered >= 48 && valueEntered <= 57));
            if (!toAllow) {
                Mojo.Controller.errorDialog('Enter a valid numeral')
            }
            return toAllow;
        }
    }
    this.txt_model = {
        'initial': '0'
    }
    
    this.date_title = $L('Today')
    
}

FirstAssistant.prototype.handleOperation = function(operator){

    //Nothing to do
    if ('0' == this.txt_model['initial']) {
        return;
    }
    
    var num = this.txt_model['initial'];
    var origDate = this.model['date'].toDateString();
    
    var subscripts = {
        '1': 'st',
        '2': 'nd',
        '3': 'rd'
    };
    var suffix = 'th';
    if (num in subscripts) {
        suffix = subscripts[num];
    }
    
    Mojo.Log.info('Doing a ' + operator + ' of ' + num + ' days');
    Mojo.Log.info('...to the date ' + this.model['date']);
    
    var newDay = eval(this.model['date'].getDate() + operator + num);
    var toFrom = operator == "+" ? 'from ' : 'to '
    
    this.model['date'].setDate(newDay);
    this.date_title = num + suffix + ' day ' + toFrom + origDate + ' is...';
    
    this.updateModels();
}

FirstAssistant.prototype.eventCreate = function(){
    Mojo.Controller.stageController.pushScene('create-event', this.model['date']);
}

FirstAssistant.prototype.propertyChangeHdl = function(event){

    if (!isSameDate(new Date(), event.value)) {
        $('date_title').update('')
    }
    
}

FirstAssistant.prototype.setup = function(){

    this.init()
    this.controller.setupWidget('date_field', undefined, this.model);
    this.controller.setupWidget('text_field', this.text_attr, this.txt_model);
    $('date_title').update(this.date_title)
    
    var model = new DateCalculatorModels(this.controller.sceneName)
        
    this.controller.setupWidget(Mojo.Menu.appMenu, DateCalculatorModels.menuAttrs, model.getAppMenuModel());
    this.controller.setupWidget(Mojo.Menu.viewMenu, undefined, model.getViewMenuModel());
    
    this.propChangeHandler = this.propertyChangeHdl.bind(this)
    this.resetHandler = this.resetButtonTap.bind(this);
    this.addHandler = this.handleOperation.bind(this, '+');
    this.subHandler = this.handleOperation.bind(this, '-');
    this.evtHandler = this.eventCreate.bind(this);
    Mojo.Event.listen($('date_field'), Mojo.Event.propertyChange, this.propChangeHandler)
    Mojo.Event.listen($('reset_btn'), Mojo.Event.tap, this.resetHandler);
    Mojo.Event.listen($('add_btn'), Mojo.Event.tap, this.addHandler);
    Mojo.Event.listen($('sub_btn'), Mojo.Event.tap, this.subHandler);
    Mojo.Event.listen($('evt_btn'), Mojo.Event.tap, this.evtHandler);
    
};

FirstAssistant.prototype.cleanup = function(event){

    Mojo.Event.stopListening($('date_field'), Mojo.Event.propertyChange, this.propChangeHandler)
    Mojo.Event.stopListening($('reset_btn'), Mojo.Event.tap, this.resetHandler);
    Mojo.Event.stopListening($('add_btn'), Mojo.Event.tap, this.addHandler);
    Mojo.Event.stopListening($('sub_btn'), Mojo.Event.tap, this.subHandler);
    Mojo.Event.stopListening($('evt_btn'), Mojo.Event.tap, this.evtHandler)
    
};
